//
//  ViewController.m
//  miniStore
//
//  Created by liliang on 13-6-13.
//  Copyright (c) 2013年 李 良. All rights reserved.
//

#import "ViewController.h"
#import "toolbox.h"
#import "MainViewController.h"
#import "SettingViewController.h"
#import "InventoryViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib

//    [self debugInspector];
}


-(void)viewDidAppear:(BOOL)animated
{
    AppController *app = [AppController shared];
    NSString *server = app.config[@"address"];
    NSString *store = app.config[@"store"];
    if (store.length == 0 || server.length == 0) {
        [self promptForAppConfig];
    }
    NSString *filePath = @"surface.json".docPath;
    self.interfaceArray=filePath.read.jsonObject;
    if (!self.interfaceArray.count) {
        [self promptForAppConfig];
    }
    [self setupView];
}

-(void)setupView
{
    [self.view removeAllSubviews];
    for (NSDictionary *current in self.interfaceArray) {
        NSString *name = current[@"name"];
        NSString *type = current[@"type"];
        int width = [current[@"width"] integerValue];
        int height = [current[@"height"] integerValue];
        int x = [current[@"x"] integerValue];
        int y = [current[@"y"] integerValue];
        CGRect rect = CGRectMake(x, y, width, height);
        if ([type isEqualToString:@"button"]) {
            UIButton *button = [[UIButton alloc] initWithFrame:rect];
            [button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button];
            button.userInfo = current;
        }
        if ([type isEqualToString:@"image"]) {
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:rect];
            NSString *imagePath=name.docPath;
            imageview.image=[UIImage imageWithContentsOfFile:imagePath];
            [self.view addSubview:imageview];
            imageview.userInfo=current;
        }
    }
}

-(void)debugInspector{
    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSArray *windows=[[UIApplication sharedApplication] windows];
        for (UIWindow *window in windows) {
            [window inspector];
        }
        [self debugInspector];
    });
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate{
    return YES;
}

-(void)onButtonClick:(UIButton *)sender
{
    NSDictionary *current = sender.userInfo;
    NSString *key = current[@"key"];
    if ([key isEqualToString:@"register"]) {
        if ([self isServerAddressSet]) {
            MainViewController *photo = [[MainViewController alloc] initWith:@"vip.json".docPath];
            if ([@"vip.json".docPath isFileOrDir]) {
                [self.navigationController pushViewController:photo animated:YES];
            }
            else
            {
                [self promptForAppConfig];
            }
        }
        else
        {
            [self promptForAppConfig];
        }
       
    }
    if([key isEqualToString:@"photograph"])
    {
        if ([self isServerAddressSet]) {
            MainViewController *photo = [[MainViewController alloc] initWith:@"defective.json".docPath];
            if ([@"defective.json".docPath isFileOrDir]) {
                [self.navigationController pushViewController:photo animated:YES];
            }
            else
            {
                [self promptForAppConfig];
            }
        }
        else
        {
            [self promptForAppConfig];
        }
    }
    if ([key isEqualToString:@"set"]) {
        [self promptForAppConfig];
    }
    if ([key isEqualToString:@"query"]) {
        InventoryViewController *inventory = [[InventoryViewController alloc] init];
        [self.navigationController pushViewController:inventory animated:YES];
    }
}

-(void)promptForAppConfig
{
    AppController *app=[AppController shared];
    SettingViewController *set = [[SettingViewController alloc] initWithSettingDictionary:app.config];
    set.delegate = self;
    [set show];
}

-(BOOL)isServerAddressSet{
    int didSet=0;
    AppController *app=[AppController shared];
    NSString *address=app.config[@"address"];
    if (address.length) {
        didSet++;
    }
    if (YES) {
        didSet++;
    }
    return didSet==2;
}

@end
